package cc.comac.saic.hkhy.jsrj.gj.inttools.parser;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cc.comac.saic.hkhy.jsrj.gj.inttools.constant.FileType;

/**
 * @author shisongyan
 * @date 2025/11/18
 */
@Component
public class ParserFactory {
    private final List<DataParser> parsers;
    private final Map<String, DataParser> parserMap;

    @Autowired
    public ParserFactory(List<DataParser> parsers) {
        this.parsers = parsers;
        parserMap = parsers.stream().collect(Collectors.toMap(DataParser::getParserType, dataParser -> dataParser, (o, o2) -> o));
    }

    public DataParser getParser(String data) {
        for (DataParser parser : parsers) {
            if (parser.supports(data)) {
                return parser;
            }
        }
        return null;
    }

    public DataParser getParser(FileType fileType) {
        return parserMap.get(fileType.getCode());
    }
}
